/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devildogs.dao;

import com.devil.dogs.beans.Airplane;
import com.devil.dogs.interfaces.DAO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 *
 * @author admin
 */
public class AirplaneDAO extends AbstractDAO implements DAO {

    private static AirplaneDAO dao;
    private static DataSource ds;
    private String tableName;

    static {
        dao = new AirplaneDAO();
    }

    protected AirplaneDAO() {
        initDataSource();
    }

    private void initDataSource() {
        try {
            InitialContext ic = new InitialContext();
            ds = (DataSource) ic.lookup("java:comp/env/jdbc/devildogs");
        } catch (NamingException ex) {
            Logger.getLogger(AirplaneDAO.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
    }

    /**
     * Returns a Singleton class Object
     *
     * @return class Object
     */
    public static AirplaneDAO getInstance() {
        return dao;
    }

    //-----------------------------------------------------------------------------

    @Override
    public DataSource getDataSource() {
        return ds;
    }

    @Override
    public String buildUpdateString(Object o, String tableName) {
        Airplane airplane = (Airplane) o;
        StringBuilder sb = new StringBuilder("Update ");
        sb.append(tableName).append(" ");
        String s = "UPDATE Airplanes SET HOBBS_LEFT_ENGINE = " + airplane.getHobbsLeftEngine()
                + ", HOBBS_LAST_OVERHAUL = " + airplane.getHobbsLastOverhaul()
                + ", DATE_LAST_OVERHAUL = '" + airplane.getDateLastOverhaul()
                + "', ANNUAL_DATE = '" + airplane.getAnnualDate()
                + "', HOBBS_LAST_HUNDRED = " + airplane.getHobbsLastHundred()
                + ", DATE_LAST_HUNDRED = '" + airplane.getDateLastHundred()
                + "', CURRENT_FUEL = " + airplane.getCurrentFuel()
                + ", CURRENT_OIL = " + airplane.getCurrentOil()
                + "Where TAILNUMBER = '" + airplane.getTailNumber() + "'";
        System.out.println("Made it to buildUpdateString");
        return s;
    }
}
